<template>
  <div class="search-result-container">
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell
        class="van-ellipsis"
        v-for="(article, index) in list"
        :key="index"
        :title="article.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '../../api/search'

export default {
  name: 'searchResult',
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1, // 页码
      perPage: 20 // 每页条目数
    }
  },
  methods: {
    async onLoad () {
      const { data: { data: { results: res } } } = await getSearchResult({
        page: this.page,
        perPage: this.perPage,
        q: this.searchText // 搜索的字符
      })
      console.log('q', res)
      // 将数据放到数据列表中
      this.list.push(...res)
      // 关闭本次的 loading
      this.loading = false
      // 判断是否还有数据
      if (res.length) {
        // 还有数据，更新获取下一页数据的页码
        this.page++
      } else {
        // 没有数据，关闭加载更多
        this.finished = true
      }
    }
  }
}
</script>

<style scoped>
.search-result-container {
  position: fixed;
  top: 54px;
  left: 0;
  right: 0;
  bottom: 0;
  overflow-y: auto;
}
</style>
